package com.spontaneapp.fragments;

import com.spontaneapp.R;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

public class SettingsFragment extends Fragment {
	
	// State
	private EditText radius;
	private Switch kmOrMile;
	private Button doneButton;
	SharedPreferences settings;
	private SharedPreferences.Editor editor;
	String tempRadius;
	boolean tempIsKm;

	public SettingsFragment(){}
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	return inflater.inflate(R.layout.fragment_settings, container, false);
    }

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		
		settings = getActivity().getPreferences(0);
		editor = settings.edit();
		
		radius = (EditText) getActivity().findViewById(R.id.settings_show_current_radius_edit_text);
		kmOrMile = (Switch) getActivity().findViewById(R.id.settings_switch);
		kmOrMile.setTextOn("Km");
		kmOrMile.setTextOff("Miles");
		doneButton = (Button) getActivity().findViewById(R.id.settings_done_button);
		
		radius.setText(""+settings.getInt("radius", 20));
		tempRadius = radius.getText().toString();
		kmOrMile.setChecked(settings.getBoolean("isKm", true));
		
		/*
		 * Listen to changes in the Switch and update the Shared preferences accordingly
		 */
		kmOrMile.setOnCheckedChangeListener(new OnCheckedChangeListener() {
		    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		    	tempIsKm = isChecked;
		    }
		});
		
		/*
		 * Listen to changes in the radius EditText and update the Shared preferences accordingly
		 */
		radius.addTextChangedListener(new TextWatcher(){
			@Override
	        public void afterTextChanged(Editable s) {
				tempRadius = radius.getText().toString();
				// if new distance is not valid pop alert dialog
				if(tempRadius == null || tempRadius == "0"){
					AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
					builder.setMessage("Please fill in a valid distance!")
					       .setCancelable(false)
					       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                dialog.dismiss();
					           }
					       });
					AlertDialog alert = builder.create();
					alert.show();
				}
	        }
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {}
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {}
		});
		
		/*
		 * when done button is pressed all we need to do is go back to last fragment
		 */
		doneButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				/*
				 * update radius if valid
				 */
				if(tempRadius == null || tempRadius == "0")
					Toast.makeText(getActivity(), "The new distance is not valid,\n radius was not saved!", Toast.LENGTH_LONG).show();
				else{
					editor.putInt("radius", Integer.parseInt(tempRadius));
				}
				/*
				 * update Km or Miles
				 */
				if(tempIsKm){
					editor.putBoolean("isKm", true);
		    	}
				else{
					editor.putBoolean("isKm", false);
				}
				editor.commit();
				getActivity().onBackPressed();
			}
		});
	}
}
